/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package lu.med.praxiswahl.wahlumed.service.beans;

import java.util.Calendar;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.ejb.EJB;
import javax.ejb.Init;
import javax.ejb.Remote;
import javax.ejb.SessionContext;
import javax.ejb.Stateless;

import lu.med.praxiswahl.wahlumed.dao.beans.daointerface.OrdonanceDAOInt;
import lu.med.praxiswahl.wahlumed.model.beans.CustomerTBL;
import lu.med.praxiswahl.wahlumed.model.beans.OrdonanceTBL;
import lu.med.praxiswahl.wahlumed.service.beans.serviceinterface.CustomerServiceInt;
import lu.med.praxiswahl.wahlumed.service.beans.serviceinterface.OrdonanceServiceInt;

/**
 * 
 * @author pkendzo
 */
@Stateless
@Remote({ OrdonanceServiceInt.class })
public class OrdonanceService implements OrdonanceServiceInt {

	private OrdonanceTBL tmpOrd;
	@EJB
	private OrdonanceDAOInt ordDao;

//	@Resource
//    private SessionContext sessionContext;
	
	@EJB
	private CustomerServiceInt cust;

	public OrdonanceService() {
		tmpOrd = new OrdonanceTBL();
	}

//	@PostConstruct
//    void initialise() {
//		cust = sessionContext.getBusinessObject(CustomerServiceInt.class);
//    }
	
	@Override
	public int createOrdonance(Long ordonanceNr, Calendar date,
			String matricule, String bezeichner, String typ, String text) {
		tmpOrd.setOrdonance_nummer(ordonanceNr);
		tmpOrd.setDatum(date);
		tmpOrd.setCustomer(cust.getCustomer(matricule));
		tmpOrd.setBezeichner(bezeichner);
		tmpOrd.setTyp(typ);
		tmpOrd.setText(text);

		ordDao.persist(tmpOrd);

		return RET_OPERATION_DONE_PROPERLY;
	}

	@Override
	public int deleteOrdonance(Long id) {
		ordDao.delete(id);

		return RET_OPERATION_DONE_PROPERLY;
	}

	@Override
	public void getOrdonance(Long id) {
		tmpOrd = ordDao.retrieveOrdonanceTemplateByID(id);

	}

	@Override
	public void getOrdonance(OrdonanceTBL ordonance) {
		tmpOrd = ordonance;

	}

	@Override
	public String getBezeichner() {
		return tmpOrd.getBezeichner();
	}

	@Override
	public CustomerTBL getPatientin() {
		return tmpOrd.getCustomer();
	}

	@Override
	public Calendar getDate() {
		return tmpOrd.getDatum();
	}

	@Override
	public Long getOrdonance_nummer() {
		return tmpOrd.getOrdonance_nummer();
	}

	@Override
	public String getText() {
		return tmpOrd.getText();
	}

	@Override
	public String getTyp() {
		return tmpOrd.getTyp();
	}

	@Override
	public Long[] listCustomerOrdonancenBy_Typ_AND_Customer(String typ,
			String matricule) {
		List<OrdonanceTBL> tmp = ordDao
				.findCustomerOrdonancenBy_Typ_AND_Customer(typ, matricule);
		Long[] ordIDs = new Long[tmp.size()];

		for (int i = 0; i < tmp.size(); i++) {
			ordIDs[i] = tmp.get(i).getOrdonance_nummer();
		}
		return ordIDs;
	}

	@Override
	public OrdonanceTBL[] getCustomerOrdonancenBy_Typ_AND_Customer(String typ,
			String matricule) {
		List<OrdonanceTBL> tmp = ordDao
				.findCustomerOrdonancenBy_Typ_AND_Customer(typ, matricule);
		OrdonanceTBL[] tmp_1 = new OrdonanceTBL[tmp.size()];

		for (int i = 0; i < tmp_1.length; i++) {
			tmp_1[i] = tmp.get(i);
		}
		return tmp_1;
	}

	@Override
	public int updateLaborCustomerOrdonance(String laborResults) {
		tmpOrd.setResult_Labor_text(laborResults);

		ordDao.update(tmpOrd);

		return RET_OPERATION_DONE_PROPERLY;
	}

	@Override
	public Long getSubPrimaryKeyIndex() {
		return ordDao.findDatabaseSubPrimaryKeyIndex();
	}

	@Override
	public String getLaborResults() {
		return tmpOrd.getResult_Labor_text();
	}

	@Override
	public void update_date(Long id, Calendar datum) {
		OrdonanceTBL tmpOrd_1 = this.ordDao.retrieveOrdonanceTemplateByID(id);
		tmpOrd_1.setDatum(datum);

		this.ordDao.update(tmpOrd_1);

	}

	@Override
	public void update_bezeichner(Long id, String bezeichner) {

		OrdonanceTBL tmpOrd_1 = this.ordDao.retrieveOrdonanceTemplateByID(id);
		tmpOrd_1.setBezeichner(bezeichner);

		this.ordDao.update(tmpOrd_1);

	}

	@Override
	public void update_text(Long id, String text) {

		OrdonanceTBL tmpOrd_1 = this.ordDao.retrieveOrdonanceTemplateByID(id);
		tmpOrd_1.setText(text);

		this.ordDao.update(tmpOrd_1);

	}

}
